package dav.mobile.garuda.states;

import java.util.Queue;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

import dav.mobile.garuda.components.Constants;
import dav.mobile.garuda.components.ResourceManager;
import dav.mobile.garuda.components.SoundManager;
import dav.mobile.garuda.states.StateMap.MapMode;
import dav.mobile.garuda.states.scene.Scene;

//VN-like scene state
public class StateScene extends State {
	//Scene data
	Queue<Scene> 	m_Scenes;
	String			m_ScenePlace;
	int 			m_SceneNum;
	TextureRegion 	m_Background;
	
	public StateScene(String name) {
		//Get Scenes
		String m_Path = Constants.GAME_SCENE_FOLDER + name + Constants.GAME_SCENE_EXTENSION;
		m_Scenes = Scene.CreateScene(m_Path);
		
		//Get scene number
		int m_Strip = name.indexOf("-");
		m_ScenePlace = name.substring(0, m_Strip);
		m_SceneNum = Integer.valueOf(name.substring(m_Strip+1));

		//Get background
		if (m_ScenePlace.equals("prolog")) m_Background 		= ResourceManager.vn_Backgrounds[Constants.GAME_PLACE_PROLOG];
		else if (m_ScenePlace.equals("epilog")) m_Background 	= ResourceManager.vn_Backgrounds[Constants.GAME_PLACE_PROLOG];
		else if (m_ScenePlace.equals("prambanan")) m_Background = ResourceManager.vn_Backgrounds[Constants.GAME_PLACE_PRAMBANAN];
		else if (m_ScenePlace.equals("bunaken")) m_Background 	= ResourceManager.vn_Backgrounds[Constants.GAME_PLACE_BUNAKEN];
		else if (m_ScenePlace.equals("borneo")) m_Background 	= ResourceManager.vn_Backgrounds[Constants.GAME_PLACE_BORNEO];
		else if (m_ScenePlace.equals("toba")) m_Background 		= ResourceManager.vn_Backgrounds[Constants.GAME_PLACE_TOBA];
	
		//Play music
		SoundManager.PlayBGM(SoundManager.m_SceneMusic);
	}
	
	@Override
	public void Update() {		
		//Go to next state if already done
		if (IsDone()) {
			//Stop bgm
			SoundManager.StopBGM(SoundManager.m_SceneMusic);
			
			if (m_ScenePlace.equals("epilog")&&m_SceneNum==5) SetState(new StateMenu());
			else if ((m_ScenePlace.equals("prolog")&&m_SceneNum==2)||(m_SceneNum==4)) SetState(new StateMap(MapMode.QUEST));
			else SetState(new StateGame(m_ScenePlace,m_SceneNum));
		}
		
		if(Gdx.input.justTouched()) {
			//If scene exists
			if (m_Scenes.peek()!=null) {
				//Pop this state
				m_Scenes.peek().Dispose();
				m_Scenes.poll();
				
				//Check next state
				if (m_Scenes.peek()==null) m_Done = true;
			} 
		}
	}

	@Override
	public void Render() {
		//Render Scene
		if (m_Scenes.peek()!=null) {	
			m_SpriteBatch.begin();
			
			//draw background
			m_SpriteBatch.draw(m_Background, 0, 0, Constants.GAME_WIDTH, Constants.GAME_HEIGHT);
			
			//sprite
			if (m_Scenes.peek().m_Right!=null) m_SpriteBatch.draw(m_Scenes.peek().m_Right, 480 + m_Scenes.peek().m_Right.getRegionWidth(), 0, -m_Scenes.peek().m_Right.getRegionWidth(), m_Scenes.peek().m_Right.getRegionHeight());
			if (m_Scenes.peek().m_Left!=null) m_SpriteBatch.draw(m_Scenes.peek().m_Left, 0, 0, m_Scenes.peek().m_Left.getRegionWidth(), m_Scenes.peek().m_Left.getRegionHeight());
			
			//text box
			m_SpriteBatch.draw(ResourceManager.vn_Textbox, Constants.SCENE_TEXTBOX_X, Constants.SCENE_TEXTBOX_Y, Constants.SCENE_TEXTBOX_WIDTH, Constants.SCENE_TEXTBOX_HEIGHT);
			
			//text
			ResourceManager.m_FontScene.drawMultiLine(m_SpriteBatch, m_Scenes.peek().m_Name, Constants.SCENE_NAME_X, Constants.SCENE_NAME_Y);
			ResourceManager.m_FontScene.drawMultiLine(m_SpriteBatch, m_Scenes.peek().m_Text, Constants.SCENE_TEXT_X, Constants.SCENE_TEXT_Y);

			m_SpriteBatch.end();
		}
	}

}
